package com.fzj.charging.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fzj.charging.common.pojo.param.GunStatusUpdateParam;
import com.fzj.charging.common.pojo.vo.StationInfoVO;
import com.fzj.charging.device.dao.mapper.ChargingGunMapper;
import com.fzj.charging.device.dao.mapper.ChargingPileMapper;
import com.fzj.charging.device.dao.mapper.ChargingStationMapper;
import com.fzj.charging.device.dao.mapper.GunUpdateStatusMapper;
import com.fzj.charging.device.dao.repository.StationRedisRepository;
import com.fzj.charging.device.pojo.dto.ChargingStationGetParam;
import com.fzj.charging.device.pojo.entity.ChargingGunInfo;
import com.fzj.charging.device.pojo.entity.ChargingPileInfo;
import com.fzj.charging.device.pojo.entity.ChargingStation;
import com.fzj.charging.device.pojo.po.GunUpdatePO;
import com.fzj.charging.device.pojo.vo.ChargingStationVO;
import com.fzj.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private GunUpdateStatusMapper gunUpdateStatusMapper;
    @Autowired
    private ChargingGunMapper chargingGunMapper;
    @Autowired
    private ChargingPileMapper chargingPileMapper;
    @Autowired
    private ChargingStationMapper chargingStationMapper;
    @Autowired
    @Qualifier("geoAndMapMany")
    private StationRedisRepository stationRedisRepository;

    @Override
    public StationInfoVO getStationInfoVO(Integer gunId) {
        log.debug("设备服务，获取场站信息业务，参数：{}",gunId);
        ChargingGunInfo chargingGunInfo = chargingGunMapper.selectById(gunId);
        ChargingPileInfo chargingPileInfo = chargingPileMapper.selectById(chargingGunInfo.getPileId());
        ChargingStation chargingStation = chargingStationMapper.selectById(chargingPileInfo.getStationId());

        StationInfoVO stationInfoVO = new StationInfoVO();
        stationInfoVO.setStationId(chargingStation.getId());
        stationInfoVO.setOperatorId(chargingStation.getOperatorId());
        log.debug("设备服务，获取场站信息业务，参数：{},获取到的场站信息结果：{}",gunId,stationInfoVO);

        return stationInfoVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        log.debug("设备服务，修改枪的状态，参数：{}",gunStatusUpdateParam);
        GunUpdatePO gunUpdatePO = new GunUpdatePO();
        gunUpdatePO.setGunStatus(gunStatusUpdateParam.getStatus());
        UpdateWrapper<GunUpdatePO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",gunStatusUpdateParam.getGunId());
        int updateRow = gunUpdateStatusMapper.update(gunUpdatePO, updateWrapper);
        log.debug("设备服务，修改枪的状态，参数：{}，数据库中受影响的行数：{}",gunStatusUpdateParam,updateRow);
        return updateRow>0;
    }

    @Override
    public List<ChargingStationVO> getStationListByCircle(ChargingStationGetParam chargingStationGetParam) {
        log.debug("根据用户位置查询附近充电站业务，参数：{}",chargingStationGetParam);
        List<ChargingStation> chargingStationList = stationRedisRepository.selectStation(chargingStationGetParam.getLongitude(), chargingStationGetParam.getLatitude(), chargingStationGetParam.getDistance());
        log.debug("根据用户位置查询附近充电站业务，参数：{},查询到的充电站集合：{}",chargingStationGetParam,chargingStationList);
        ArrayList<ChargingStationVO> chargingStationVOList = new ArrayList<>();
        //chargingStationVOList.addAll(chargingStationList);
        if(chargingStationList!=null&&!chargingStationList.isEmpty()){
            for (ChargingStation chargingStation : chargingStationList) {
                ChargingStationVO chargingStationVO = new ChargingStationVO();
                BeanUtils.copyProperties(chargingStation,chargingStationVO);
                chargingStationVOList.add(chargingStationVO);
            }
            log.debug("根据用户位置查询附近充电站业务，参数：{},返回的充电站集合：{}",chargingStationGetParam,chargingStationList);
            return chargingStationVOList;
        }else{
            return Collections.emptyList();
        }

    }
}
